package net.zio.kamenriderzioaddon.ridersystem.registry;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.item.ItemStack;
import net.zio.kamenriderzioaddon.enums.SoundType;
import net.zio.kamenriderzioaddon.init.KamenRiderZioAddonModGeckoLibArmors;
import net.zio.kamenriderzioaddon.init.KamenRiderZioAddonModItems;
import net.zio.kamenriderzioaddon.init.KamenRiderZioAddonModSounds;
import net.zio.kamenriderzioaddon.ridersystem.behavior.IHenshinBaseBehavior;
import net.zio.kamenriderzioaddon.ridersystem.behavior.impl.GeizHenshinBehavior;
import net.zio.kamenriderzioaddon.ridersystem.behavior.impl.GeizMajestyBehavior;
import net.zio.kamenriderzioaddon.ridersystem.behavior.impl.GeizReviveGoretsuBehavior;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;

/**
 * 骑士注册器，用于注册骑士及其相关配置。
 *
 * @author QianYunYi
 */
public class RiderRegistry {

    /**
     * 注册所有骑士配置
     */
    public static void registerAllRiders() {
        registerGeiz();
        // 注册其他骑士...
    }

    /**
     * 注册 Geiz 骑士配置
     */
    private static void registerGeiz() {
        Map<SoundType, SoundEvent> baseSounds = new HashMap<>();
        baseSounds.put(SoundType.STANDBY,
                KamenRiderZioAddonModSounds.REGISTRY.get(new ResourceLocation("kamen_rider_zio_addon:geiz_standby_sound"))
        );
        baseSounds.put(SoundType.DRIVER,
                KamenRiderZioAddonModSounds.REGISTRY.get(new ResourceLocation("kamen_rider_zio_addon:zikudriver"))
        );
        baseSounds.put(SoundType.TRANSFORM,
                KamenRiderZioAddonModSounds.REGISTRY.get(new ResourceLocation("kamen_rider_zio_addon:geiz_complete"))
        );

        IHenshinBaseBehavior geizBehavior = new GeizHenshinBehavior();

        // 创建变身条件判断
        Predicate<Player> transformCondition = player -> {
            // 检查腰带是否装备
            if (player.getItemBySlot(EquipmentSlot.LEGS).getItem() !=
                    KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.ZIKUDRIVER_LEGGINGS.get()) {
                return false;
            }

            // 检查主手是否有变身道具
            ItemStack mainHand = player.getMainHandItem();

            return mainHand.getItem() == KamenRiderZioAddonModItems.GEIZWATCH.get() ;

        };

        FullRiderConfig geizConfig = new FullRiderConfig(
                "Geiz",
                new Item[]{
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.GEIZ_HELMET.get(),
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.GEIZ_CHESTPLATE.get(),
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.ZIKUDRIVERWITHGEIZWATCH_LEGGINGS.get(),
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.GEIZ_BOOTS.get()
                },
                transformCondition,
                KamenRiderZioAddonModItems.GEIZWATCH.get(),
                null,
                baseSounds,
                geizBehavior
        );

        // 注册 Geiz 强化形态
        registerGeizMajesty(geizConfig);
        registerGeizReviveGoretsu(geizConfig);
    }

    // 注册 Geiz 强化形态
    private static void registerGeizReviveGoretsu(FullRiderConfig geizConfig) {
        Map<SoundType, SoundEvent> awakeningSounds = new HashMap<>();
        awakeningSounds.put(SoundType.STANDBY,
                KamenRiderZioAddonModSounds.REGISTRY.get(new ResourceLocation("kamen_rider_zio_addon:geiz_revive_standby"))
        );
        awakeningSounds.put(SoundType.DRIVER,
                KamenRiderZioAddonModSounds.REGISTRY.get(new ResourceLocation("kamen_rider_zio_addon:zikudriver"))
        );
        awakeningSounds.put(SoundType.TRANSFORM,
                KamenRiderZioAddonModSounds.REGISTRY.get(new ResourceLocation("kamen_rider_zio_addon:geiz_revive_complete"))
        );

        // 创建强化形态变身条件判断
        Predicate<Player> transformCondition = player -> {

            // 检查是否佩戴对应腰带和持有对应变身道具
            if (player.getItemBySlot(EquipmentSlot.LEGS).getItem() ==
                    KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.ZIKUDRIVER_LEGGINGS.get()) {
                return player.getItemBySlot(EquipmentSlot.MAINHAND).getItem() == KamenRiderZioAddonModItems.GEIZWATCH.get() &&
                        player.getItemBySlot(EquipmentSlot.OFFHAND).getItem() == KamenRiderZioAddonModItems.GEIZ_REVIVE_GORETSU_WATCH.get();
            } else if (player.getItemBySlot(EquipmentSlot.LEGS).getItem() ==
                    KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.ZIKUDRIVERWITHGEIZWATCH_LEGGINGS.get()) {
                return player.getItemBySlot(EquipmentSlot.OFFHAND).getItem() == KamenRiderZioAddonModItems.GEIZ_REVIVE_GORETSU_WATCH.get();
            }
            return false;

        };
        // 添加强化形态配置 (GEIZ_REVIVE_GORETSU)
        geizConfig.addAwakeningConfig(
                "GeizReviveGoretsu",
                new Item[]{
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.GEIZ_REVIVE_GORETSU_HELMET.get(),
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.GEIZ_REVIVE_GORETSU_CHESTPLATE.get(),
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.ZIKUDRIVERWITHGEIZREVIVEGORETSUWATCH_LEGGINGS.get(),
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.GEIZ_REVIVE_GORETSU_BOOTS.get()
                },
                transformCondition,
                // 主手道具
                KamenRiderZioAddonModItems.GEIZWATCH.get(),
                // 副手道具
                KamenRiderZioAddonModItems.GEIZ_REVIVE_GORETSU_WATCH.get(),
                awakeningSounds,
                new GeizReviveGoretsuBehavior()
        );
    }

    // 注册 Geiz 强化形态
    private static void registerGeizMajesty(FullRiderConfig geizConfig) {
        Map<SoundType, SoundEvent> awakeningSounds = new HashMap<>();
        awakeningSounds.put(SoundType.STANDBY,
                KamenRiderZioAddonModSounds.REGISTRY.get(new ResourceLocation("kamen_rider_zio_addon:geiz_majesty_standby"))
        );
        awakeningSounds.put(SoundType.DRIVER,
                KamenRiderZioAddonModSounds.REGISTRY.get(new ResourceLocation("kamen_rider_zio_addon:zikudriver"))
        );
        awakeningSounds.put(SoundType.TRANSFORM,
                KamenRiderZioAddonModSounds.REGISTRY.get(new ResourceLocation("kamen_rider_zio_addon:geiz_majesty_complete"))
        );

        // 创建强化形态变身条件判断
        Predicate<Player> transformCondition = player -> {

            // 检查是否佩戴对应腰带和持有对应变身道具
            if (player.getItemBySlot(EquipmentSlot.LEGS).getItem() ==
                    KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.ZIKUDRIVER_LEGGINGS.get()) {
                return player.getItemBySlot(EquipmentSlot.MAINHAND).getItem() == KamenRiderZioAddonModItems.GEIZWATCH.get() &&
                        player.getItemBySlot(EquipmentSlot.OFFHAND).getItem() == KamenRiderZioAddonModItems.GEIZ_MAJESTY_WATCH.get();
            } else if (player.getItemBySlot(EquipmentSlot.LEGS).getItem() ==
                    KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.ZIKUDRIVERWITHGEIZWATCH_LEGGINGS.get()) {
                return player.getItemBySlot(EquipmentSlot.OFFHAND).getItem() == KamenRiderZioAddonModItems.GEIZ_MAJESTY_WATCH.get();
            }
            return false;

        };
        // 添加强化形态配置 (Geiz Majesty)
        geizConfig.addAwakeningConfig(
                "GeizMajesty",
                new Item[]{
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.GEIZMAJESTY_HELMET.get(),
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.GEIZMAJESTY_CHESTPLATE.get(),
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.ZIKUDRIVERWITHGEIZMAJESTYWATCH_LEGGINGS.get(),
                        KamenRiderZioAddonModGeckoLibArmors.GeckoLibArmorItems.GEIZMAJESTY_BOOTS.get()
                },
                transformCondition,
                // 主手道具
                KamenRiderZioAddonModItems.GEIZWATCH.get(),
                // 副手道具
                KamenRiderZioAddonModItems.GEIZ_MAJESTY_WATCH.get(),
                awakeningSounds,
                new GeizMajestyBehavior()
        );
    }
}